
import 'package:common_utils/common_utils.dart';
import 'package:extended_text/extended_text.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:osc/common/MyTextStyle.dart';
import 'package:osc/models/entity/osc_message_mention_entity.dart';
import 'package:osc/res/colors.dart';
import 'package:osc/res/dimens.dart';
import 'package:osc/utils/navigator_util.dart';
import 'package:osc/utils/pd_util.dart';
import 'package:osc/utils/special_text/my_special_text_span_builder.dart';
import 'package:osc/utils/string_util.dart';

///消息内容
class MessageContent extends StatefulWidget {
  final OscMessageMentionEntity item;

  const MessageContent({Key key, @required this.item}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return new MessageContentState(item);
  }
}

class MessageContentState extends State<MessageContent> {
  final OscMessageMentionEntity item;
  MessageContentState(this.item);

  double paddingParentHorizontal = Dimens.gap_activity_horizontal;
  double paddingChildHorizontal = Dimens.gap_view_normal;
  double paddingChildVertical = Dimens.gap_view_normal;



  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Expanded(
        child: Container(
          margin: EdgeInsets.only(left: paddingChildHorizontal),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Text(item.author.name, style: MyTextStyle.USER_LIST_TITLE,),
              buildContent(),
              buildAbout(),
              buildBottom(),
            ],
          ),
        ),
    );
  }

  buildContent(){
    return Padding(
      padding: EdgeInsets.symmetric(vertical: paddingChildVertical),
      child: buildContentText(item.content),
    );
  }

  buildAbout(){
    OscMessageOrigin about = item.origin;
    if(about == null || StringUtil.isEmpty(about.desc)) {
      return Offstage(
        offstage: true,
      );
    } else {
      return Container(
        width: ScreenUtil.getScreenW(context),
        color: Colours.bg_tweet_about,
        padding: EdgeInsets.symmetric(horizontal: paddingParentHorizontal,
            vertical: paddingChildVertical),
        child: buildContentText(about.desc),
      );
    }
  }

  buildContentText(String text){
    return ExtendedText(
      text,
      style: MyTextStyle.TWEET_CONTENT,
      onSpecialTextTap: (dynamic parameter) {},
      specialTextSpanBuilder: MySpecialTextSpanBuilder(),
      overflow: TextOverflow.ellipsis,
      maxLines: 10,
    );
  }

  buildBottom(){
    String time = TimelineUtil.formatByDateTime(DateTime.parse(item.pubDate), dayFormat: DayFormat.Full);
    String appClient = "";
    if(item.appClient == 3){
      appClient = "Android";
    } else if(item.appClient == 4){
      appClient = "iPhone";
    }
    return Padding(
      padding: EdgeInsets.symmetric(vertical: paddingChildVertical),
      child: Row(
        children: <Widget>[
          Text("$time  $appClient", style: MyTextStyle.LIST_BOTTOM,),
          buildBottomItem("ic_comment_30", "${item.commentCount}"),
        ],
      ),
    );
  }

  buildBottomItem(String imageName, String text){
    return Expanded(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: <Widget>[
            Image.asset(PdUtil.getImagePath(imageName), width: Dimens.view_item_icon_height_smaller, fit: BoxFit.fitWidth,),
            Text(" $text", style: MyTextStyle.LIST_BOTTOM,)
          ],
        ),
    );
  }

  jump2Page(Widget widget){
    NavigatorUtil.pushPage(context, widget);
  }
}
